package com.ddstation.app.report.controller;

import com.ddstation.app.report.model.ReportUploadModel;
import com.ddstation.app.report.service.ReportUploadService;
import com.ddstation.common.entity.DdPictureEntity;
import com.ddstation.common.mapper.DdPictureMapper;
import com.ddstation.common.util.AliCloudOssUtil;
import com.ddstation.common.util.CheckUtil;
import com.ddstation.crud.entity.DtInvsReportUploadEntity;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AppReportUploadController {

    @Inject private ReportUploadService reportUploadService;
    @Inject private DdPictureMapper ddPictureMapper;


    /**
     * (污水检测)获取历史记录
     * @param params
     * @return
     */
    @RequestMapping(value="appinvs_report_upload.do/history", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> invsWushuiCheckHistoryEvent(
            @RequestBody Map<String, Object> params) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
                || CheckUtil.isEmpty(params.get("reportType"))) {
            return retMap;
        }
        String institutionId = params.get("institutionId").toString();
//		String institutionId = "00000012";
        String pageSize = params.get("pageSize").toString();
        String pageNo = params.get("pageNo").toString();
        Integer reportType = Integer.parseInt(params.get("reportType").toString());

        retMap.put("itemList",reportUploadService.selectByHistory(institutionId,reportType,Integer.parseInt(pageNo),Integer.parseInt(pageSize)));
        retMap.put("errCode", 0);
        retMap.put("errorMsg", "");
        return retMap;

    }

    /**
     * (污水检测)报告详情
     * @param params
     * @return
     */
    @RequestMapping(value="appinvs_report_upload.do/detail", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> invsWushuiCheckDetailEvent(
            @RequestBody Map<String, Object> params) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
                || CheckUtil.isEmpty(params.get("id"))) {
            return retMap;
        }
        String institutionId = params.get("institutionId").toString();
        Integer id = Integer.parseInt(params.get("id").toString());
        DtInvsReportUploadEntity entity = new DtInvsReportUploadEntity();
        entity.setInstitutionId(institutionId);
        entity.setId(id);
        DtInvsReportUploadEntity infoEntity =reportUploadService.queryReportInfo(entity);
        retMap.put("info", infoEntity);


        List<String> thumbImgList = new ArrayList<String>();
        List<String> originImgList = new ArrayList<String>();
        if (infoEntity.getReportImgId()!=null) {
            for (String m_id:infoEntity.getReportImgId().split(",")) {
                if (!CheckUtil.isEmpty(m_id)) {
                    DdPictureEntity picture = ddPictureMapper.selectOne(m_id);
                    if (picture.getPath().endsWith("pdf") || picture.getPath().endsWith("PDF")
                            || picture.getPath().endsWith("xls") || picture.getPath().endsWith("xlsx")
                            || picture.getPath().endsWith("XLS") || picture.getPath().endsWith("XLSX")
                            || picture.getPath().endsWith("doc") || picture.getPath().endsWith("docx")
                            || picture.getPath().endsWith("DOC") || picture.getPath().endsWith("DOCX")) {
                        continue;
                    }
                    thumbImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@100h"));
                    originImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@800h"));
                    //originImgList.add(AliCloudOssUtil.getOriginImgUrl(null, picture.getPath()));
                }
            }
        }
        retMap.put("originImgList",originImgList);
        retMap.put("thumbImgList",thumbImgList);
        retMap.put("errCode", 0);
        retMap.put("errorMsg", "");
        return retMap;

    }

    /**
     * (污水检测)上传记录
     * @param params
     * @return
     */
    @RequestMapping(value="appinvs_report_upload.do/save", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> invsWushuiCheckSaveEvent(
            @RequestBody Map<String, Object> params) {

        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
                || CheckUtil.isEmpty(params.get("reportType"))
                || CheckUtil.isEmpty(params.get("currentDate"))
                || CheckUtil.isEmpty(params.get("isPass"))
                || CheckUtil.isEmpty(params.get("attachmentId"))) {
            return retMap;
        }
        Integer reportType = Integer.parseInt(params.get("reportType").toString());
        String institutionId = params.get("institutionId").toString();
        String doctorId = params.get("doctorId").toString();
        String currentDate = params.get("currentDate").toString();
        Integer isPass = "true".equals(params.get("isPass").toString())?1:2;
        String attachmentId = params.get("attachmentId").toString();
        ReportUploadModel reportInfo = new ReportUploadModel();
        reportInfo.setCheckDateStr(currentDate);
        reportInfo.setInstitutionId(institutionId);
        reportInfo.setDoctorId(Integer.parseInt(doctorId));
        reportInfo.setReportType(reportType);
        reportInfo.setQualifiedFlg(isPass);
        reportInfo.setReportImgId(attachmentId);
        reportUploadService.saveReportInfo(reportInfo);
        retMap.put("errCode", 0);
        retMap.put("errorMsg", "");
        return retMap;

    }

    /**
     * 保存报告
     * @throws ParseException
     */
    @RequestMapping(value ="/invsapp_uploadReportMobile.do", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> uploadReportMobileEvent(
            @RequestBody Map<String, Object> params) throws ParseException {

        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);

        if (params.isEmpty() || CheckUtil.isEmpty(params.get("reportInfo")) || CheckUtil.isEmpty(params.get("institutionId"))) {
            return retMap;
        }
        String institutionId = params.get("institutionId").toString();
        ReportUploadModel reportInfo = (ReportUploadModel)JSONObject.toBean(JSONObject.fromObject(params.get("reportInfo")), ReportUploadModel.class);
        reportInfo.setInstitutionId(institutionId);
        reportUploadService.saveReportInfo(reportInfo);

        retMap.put("errCode", 0);

        return retMap;
    }
}
